package com.atzongmaotan.reggie.controller;

import com.atzongmaotan.reggie.common.R;
import com.atzongmaotan.reggie.entity.Employee;
import com.atzongmaotan.reggie.service.EmployeeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * ClassName:    EmployeeController
 * Package:  com.atzongmaotan.controller
 * Description:
 *
 * @Author: interface
 * @Create: 2024/6/11 - 下午7:20
 * @Version: v1.0
 */
@Slf4j
@RestController
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        //密码加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //查询用户
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);
        //判断用户是否存在
        if (emp == null) {
            return R.error("登陆失败");
        }
        //密码比对
        if (!password.equals(emp.getPassword())) {
            return R.error("登陆失败");
        }
        //查看员工状态
        if (emp.getStatus() == 0){
            return R.error("账号已禁用");
        }
        //存入Session
        HttpSession session = request.getSession();
        session.setAttribute("employee",emp.getId());

        return R.success(emp);
    }
    @PostMapping("logout")
    public R<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }
    @PostMapping
    public R<String> add(HttpServletRequest request, @RequestBody Employee employee) {
       log.info("新增员工信息:{}",employee.toString());
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        /*
            使用mybatisplus的自动填充功能  @TableField(fill = FieldFill.INSERT) @Component public class MyMetaObjectHandler implements MetaObjectHandler {}
            employee.setCreateTime(LocalDateTime.now()); //创建时间
            employee.setUpdateTime(LocalDateTime.now()); //更新时间
            Long empId = (Long) request.getSession().getAttribute("employee");
            employee.setCreateUser(empId); //创建人
            employee.setUpdateUser(empId); //更新人
        */
        employeeService.save(employee);
        /*
            try {
                employeeService.save(employee);
            } catch (Exception e) {
                log.info("新增员工失败:{}",e.getMessage());
                return R.error(e.getMessage());
            }
            可以使用全局异常代理
         */

        return R.success("新增员工成功");
    }
    @GetMapping("page")
    public R<Page> list(@RequestParam("page") Integer pageNum, Integer pageSize, String name) {
       log.info("page={},pageSize={},name={}",pageNum,pageSize,name);
       Page<Employee> page = new Page<>(pageNum, pageSize);
       //构造条件查询构造器
       LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
       //添加过滤条件
       queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getUsername,name);
       //添加排序条件
       queryWrapper.orderByDesc(Employee::getUpdateTime);
       //执行查询
       employeeService.page(page, queryWrapper);
       return R.success(page);
    }
    @PutMapping
    public R<String> update(HttpServletRequest request, @RequestBody Employee employee) {
        log.info("修改:{}",employee.toString());
        //Long empId = (Long) request.getSession().getAttribute("employee");
       // employee.setUpdateTime(LocalDateTime.now());
        //employee.setUpdateUser(empId);
        employeeService.updateById(employee);
        return R.success("修改用户信息成功");
    }
    @GetMapping("{id}")
    public R<Employee> getById(@PathVariable("id") Long empId) {
        log.info("查询员工信息:{}",empId);
        Employee employee = employeeService.getById(empId);
        if (employee != null) {
            return R.success(employee);
        }
        return R.error("没有查询到员工信息");
    }


}
